package com.mediacloud.app.newsmodule.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import com.mediacloud.app.newsmodule.R;
import com.utils.RxUtils;

public class AutoRecyclerView extends RecyclerView {
    public int keepTime = 3000;
    public int maxSize = Integer.MAX_VALUE;

    private LinearLayoutManager mLinearLayoutManager;
    public boolean isAutoNext;
    private Runnable action;
    public boolean isStop = false;

    private int changePosition;

    private boolean isEmbed = false;

    public AutoRecyclerView(@NonNull Context context) {
        super(context);
    }

    public AutoRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public AutoRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoRecyclerView);
        isEmbed = a.getBoolean(R.styleable.AutoRecyclerView_isEmbed, false);
        a.recycle();
        setEmbed(isEmbed);
    }

    public void setEmbed(boolean isEmbed) {
        try {
            this.isEmbed = isEmbed;
            if (isEmbed) {
                setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);
            }
        } catch (Exception e) {

        }

    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        try {
            if (isEmbed) {
                int expandSpec = View.MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, View.MeasureSpec.AT_MOST);
                super.onMeasure(widthSpec, expandSpec);
                setNestedScrollingEnabled(false);
            } else {
                super.onMeasure(widthSpec, heightSpec);
            }
        } catch (Exception e) {

        }
    }

  //  public RxUtils.CallBack<Integer> autoMoveCallBack;

    private void moveNext() {
        removeAction();
        if (isStop) {
            return;
        }
        action = new Runnable() {
            @Override
            public void run() {
                try {
                    if (isStop) {
                        return;
                    }
                    if (getLayoutManager() != null && getLayoutManager() instanceof LinearLayoutManager) {
                        mLinearLayoutManager = (LinearLayoutManager) getLayoutManager();
                    }
                    if (mLinearLayoutManager != null && isAutoNext && !isStop) {
                        changePosition = mLinearLayoutManager.findFirstVisibleItemPosition() + 1;
                        if (changePosition > maxSize - 1) {
                            scrollToPosition(0);
                           /* if (autoMoveCallBack != null) {
                                autoMoveCallBack.call(1, 0);
                            }*/
                        } else {
                            mLinearLayoutManager.startSmoothScroll(setSmoothScroller(changePosition));
                            /*if (autoMoveCallBack != null) {
                                autoMoveCallBack.call(1, changePosition);
                            }*/
                            // smoothScrollToPosition(changePosition);
                        }

                    }
                    if (isAutoNext && !isStop) {
                        moveNext();
                    }
                } catch (Exception e) {

                }
            }
        };
        if (isStop) {
            return;
        }
        postDelayed(action, keepTime);
    }

    LinearSmoothScroller smoothScroller;

    private LinearSmoothScroller setSmoothScroller(int position) {
        if (smoothScroller == null) {
            smoothScroller = new androidx.recyclerview.widget.LinearSmoothScroller(getContext()) {//this是Context

                @Override
                protected int getHorizontalSnapPreference() {
                    return SNAP_TO_START;
                }

                @Override
                protected int getVerticalSnapPreference() {
                    return SNAP_TO_START;
                }
            };
        }
        smoothScroller.setTargetPosition(position);
        return smoothScroller;
    }

    public void setAutoNext(boolean isAutoNext) {
        try {
            this.isAutoNext = isAutoNext;
            if (this.isAutoNext) {
                isStop = false;
                removeAction();
                moveNext();
            } else {
                isStop = true;
                removeAction();
            }
        } catch (Exception e) {

        }
    }

    private void removeAction() {
        try {
            if (action != null) {
                removeCallbacks(action);
                action = null;
            }
        } catch (Exception e) {

        }
    }

    public boolean isTemp = false;
    private float tempX = 0f;
    private float tempY = 0f;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                removeAction();
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (isAutoNext) {
                    moveNext();
                }
                break;
        }
        try {
            if (isTemp) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        tempX = event.getRawX();
                        tempY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (Math.abs(event.getRawX() - tempX) > Math.abs(event.getRawY() - tempY)) {
                            getParent().requestDisallowInterceptTouchEvent(true);
                        }
                        tempX = event.getRawX();
                        tempY = event.getRawY();
                        break;
                }
            }
        } catch (Exception e) {

        }
        return super.dispatchTouchEvent(event);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (isAutoNext) {
            isStop = false;
            moveNext();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        isStop = true;
        removeAction();
    }
}
